package edu.mymvc.common.myException;

import edu.mymvc.common.R;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

//全局处理异常
@ControllerAdvice(annotations = {Controller.class , RestController.class})
@ResponseBody
public class GlobalException {
    @ExceptionHandler(CustomerException.class)
    public R myException(CustomerException ex){
        String msg = ex.getMessage();
        return R.error(msg);
    }
    //可自定义，例如捕获算术异常 : 1 / 0;
//    @ExceptionHandler(ArithmeticException.class)
//        public R MyArithmeticException(ArithmeticException ex){
//        return R.error("算术异常");
//    }
}
